<?php


namespace app\api\controller;


use app\api\job\SendNotify;
use app\common\controller\ActionLogs;
use app\common\controller\CommonCallpolice;
use app\common\model\CardConsume;
use app\common\model\CardUserBonus;
use app\common\model\CardUserFee;
use app\common\model\CardUserRecLog;
use app\common\model\Merchant;
use app\common\model\Paylog;
use think\Controller;

class CreateNotify extends Controller
{
    //支付通知拼装
    public function pay_notify($param)
    {
        $Paylog = new Paylog();
        $payinfo = $Paylog->where('orderid', $param['out_trade_no'])->find();
        if ($payinfo['pay_statu'] == 1) {
            if (!empty($payinfo['cashier'])) {
                $device_id = $payinfo['cashier'];

            } elseif (!empty($payinfo['d_cashier'])) {
                $device_id = $payinfo['d_cashier'];

            } else {
                $device_id = "";
            }
            $arrc['return_code'] = "SUCCESS";
            $arrc['result_code'] = "SUCCESS";
            $arrc['return_msg'] = "支付成功";
            $arrc['result_msg'] = "支付成功";
            $arrc['notify_category'] = "pay";
            $arrc['out_trade_no'] = $payinfo['orderid'];
            $arrc['trade_no'] = $payinfo['trade_no'];
            $arrc['order_no'] = $payinfo['orderno'];
            $arrc['total_fee'] = $payinfo['price'];//实付金额
            $arrc['channel'] = $payinfo['pay_fs'];//实付金额
            $arrc['merchantname'] = getmerchant($payinfo['mid']);//商户名称
            $arrc['ought_fee'] = $payinfo['yf_price'];//应付金额
            $arrc['gel_fee'] = $payinfo['zk_price'];//折扣金额
            $arrc['dis_card_zk'] = $payinfo['dis_card_zk'];        //会员卡折扣     field('dis_card_zk,vip_card_zk,gel_card_zk,hx_card')->
            $arrc['vip_card_zk'] = $payinfo['vip_card_zk'];        //等级折扣
            $arrc['gel_card_zk'] = $payinfo['gel_card_zk'];        //卡券抵扣
            $arrc['service_charge'] = $payinfo['service_charge'];        //手续费
            $arrc['end_time'] = !empty($payinfo['pay_time']) ? $payinfo['pay_time'] : "";//支付时间
            $arrc['gel_info'] = getdeduzkinfo($payinfo['hx_card'], $payinfo['gel_card_zk']);        //抵扣明细
            $arrc['hx_card'] = $payinfo['hx_card'];        //抵扣明细
            $arrc['openid'] = $payinfo['openid'];       //抵扣明细
            $arrc['sub_openid'] = $payinfo['sub_openid'];       //抵扣明细
            $arrc['attach'] = $payinfo['attach'];       //抵扣明细
            $arrc['device_id'] = $device_id;     //收银员
            $arrc['merchantid'] = $param['merchantid'];
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = '支付成功';
            $SendNotify = new SendNotify();
            $s = $SendNotify->task_biz_notify($arrc);


        }

    }

    //会员卡领卡信息异步通知
    public function cardrec_notify($param)
    {


    }

    //积分变动通知

    public function bonus_rec_notify($param)
    {
        $CardUserBonus = new CardUserBonus();
        $map['bus_orderid'] = $param['bus_orderid'];
        $recinfo = $CardUserBonus->where($map)->find();

        $notify_param['card_id'] = $recinfo['card_id'];
        $notify_param['code'] = $recinfo['code'];
        $notify_param['openid'] = $recinfo['openid'];
        $notify_param['bus_orderid'] = $param['bus_orderid'];
        $notify_param['action_user'] = $recinfo['action_user'];
        $notify_param['action_time'] = $recinfo['create_time'];
        $notify_param['bonus'] = $recinfo['num'];
        $notify_param['pay_cate'] = $recinfo['pay_cate'];   //支付方式
        $notify_param['cate'] = $recinfo['cate'];           //类别
        $notify_param['source'] = $recinfo['source'];       //渠道
        $notify_param['kuantai'] = $recinfo['kuantai'];     //款台
        $notify_param['remark'] = $recinfo['remark'];       //备注
        $notify_param['return_code'] = "SUCCESS";
        $notify_param['return_msg'] = '';
        $notify_param['result_code'] = 'SUCCESS';
        $notify_param['result_msg'] = '充值成功';

        $notify_param['notify_category'] = "bonus_rec";
        $notify_param['merchantid'] = $param['merchantid'];
        $SendNotify = new SendNotify();
        $SendNotify->task_biz_notify($notify_param);
        return $notify_param;


    }

    public function bonus_con_notify($param)
    {
        $CardUserBonus = new CardUserBonus();
        $map['bus_orderid'] = $param['bus_orderid'];
        $recinfo = $CardUserBonus->where($map)->find();

        $notify_param['card_id'] = $recinfo['card_id'];
        $notify_param['code'] = $recinfo['code'];
        $notify_param['openid'] = $recinfo['openid'];           //openid
        $notify_param['bus_orderid'] = $param['bus_orderid'];   //业务单号
        $notify_param['action_user'] = $recinfo['action_user']; //操作人
        $notify_param['action_time'] = $recinfo['create_time']; //时间
        $notify_param['bonus'] = $recinfo['num'];           //数量
        $notify_param['pay_cate'] = $recinfo['pay_cate'];   //支付方式
        $notify_param['cate'] = $recinfo['cate'];           //类别
        $notify_param['source'] = $recinfo['source'];       //渠道
        $notify_param['kuantai'] = $recinfo['kuantai'];     //款台
        $notify_param['remark'] = $recinfo['remark'];       //备注
        $notify_param['return_code'] = "SUCCESS";
        $notify_param['return_msg'] = '';
        $notify_param['result_code'] = 'SUCCESS';
        $notify_param['result_msg'] = '消费成功';

        $notify_param['notify_category'] = "bonus_con";
        $notify_param['merchantid'] = $param['merchantid'];
        $SendNotify = new SendNotify();
        $SendNotify->task_biz_notify($notify_param);
        return $notify_param;


    }

    //余额增加通知
    public function balance_rec_notify($bus_orderid)
    {

        $CardUserFee = new CardUserFee();
        $CardUserBonus = new CardUserBonus();
        $CardUserRecLog = new CardUserRecLog();
        $map['bus_orderid'] = $bus_orderid;
        $cardreclog = $CardUserRecLog->where($map)->find();
        $givefee = $CardUserFee->where(['id' => $cardreclog['give_fee_id']])->value('num');
        $recinfo = $CardUserFee->where(['id' => $cardreclog['rec_fee_id']])->find();
        $bonus = $CardUserBonus->where(['id' => $cardreclog['bonus_id']])->value('num');
        if($cardreclog['orderid']){
            $Paylog=new Paylog();
            $orderno=$Paylog->where(['orderid'=>$cardreclog['orderid']])->value('orderno');
            $notify_param['order_no'] =empty($orderno)?"":$orderno;
        }else{
            $notify_param['order_no'] ="";
        }

        $notify_param['orderid'] = $recinfo['orderid'];
        $notify_param['balance'] = $givefee + $recinfo['num'];
        $notify_param['give_balance'] = empty($givefee)?"0":$givefee;
        $notify_param['rec_balance'] = $recinfo['num'];
        $notify_param['give_id'] = $cardreclog['give_id'];
        $notify_param['channel'] = $cardreclog['pay_cate'];
        $notify_param['bonus'] = empty($bonus)?"0":$bonus;;
        $notify_param['card_id'] = $recinfo['card_id'];
        $notify_param['code'] = $recinfo['code'];
        $notify_param['openid'] = $recinfo['openid'];
        //END
        $notify_param['pay_cate'] = $recinfo['pay_cate'];   //支付方式
        $notify_param['cate'] = $recinfo['cate'];           //类别
        $notify_param['source'] = $recinfo['source'];       //渠道
        $notify_param['kuantai'] = $recinfo['kuantai'];     //款台
        $notify_param['remark'] = $recinfo['remark'];       //备注
        //END
        $notify_param['bus_orderid'] = $bus_orderid;
        $notify_param['action_user'] = $cardreclog['action_user'];
        $notify_param['action_time'] = $cardreclog['create_time'];
        $notify_param['notify_category'] = "balance_rec";
        $notify_param['merchantid'] = $cardreclog['mid'];
        $notify_param['return_code'] = "SUCCESS";
        $notify_param['return_msg'] = '';
        $notify_param['result_code'] = 'SUCCESS';
        $notify_param['result_msg'] = '充值成功';
        $SendNotify = new SendNotify();
        $res = $SendNotify->task_biz_notify($notify_param);
        return $notify_param;
    }

    public function balance_con_notify($notify_param)
    {
        $CardUserFee = new CardUserFee();
        $recinfo = $CardUserFee->where(['bus_orderid' => $notify_param['bus_orderid']])->find();
        $notify_param['orderid'] = $recinfo['orderid'];
        $notify_param['card_id'] = $recinfo['card_id'];
        $notify_param['code'] = $recinfo['code'];
        $notify_param['openid'] = $recinfo['openid'];
        $notify_param['balance'] = $recinfo['num'];
        $notify_param['notify_category'] = "balance_con";
        $notify_param['merchantid'] = $recinfo['mid'];
        //END
        $notify_param['pay_cate'] = $recinfo['pay_cate'];   //支付方式
        $notify_param['cate'] = $recinfo['cate'];           //类别
        $notify_param['source'] = $recinfo['source'];       //渠道
        $notify_param['kuantai'] = $recinfo['kuantai'];     //款台
        $notify_param['remark'] = $recinfo['remark'];       //备注
        $SendNotify = new SendNotify();
        $SendNotify->task_biz_notify($notify_param);

        return $notify_param;
    }

    //券核销通知
    public function quancon_notify($param)
    {
        $CardConsume = new CardConsume();
        $info = $CardConsume->where(['code' => $param['code']])->find();
        if ($info) {
            $notify_param['card_type'] = $info['card_type'];
            $notify_param['notify_category'] = "quan_con";
            $notify_param['merchantid'] = $param['merchantid'];
            $notify_param['code'] = $param['code'];
            $notify_param['con_user'] = $info['con_username'];
            $notify_param['con_time'] = $info['create_time'];
            $notify_param['source'] = $info['source'];
            $notify_param['card_id'] = $info['card_id'];
            $notify_param['openid'] = $info['openid'];
            $SendNotify = new SendNotify();
            $SendNotify->task_biz_notify($notify_param);
        }

    }

    //执行异步通知
    public function task_notify($data)
    {
        $datas = json_decode($data, true);
//        $datas = $data;
        if (isset($datas['merchantid'])) {

            $Merchant = new Merchant();
            $notify_url = $Merchant->where('id', $datas['merchantid'])->value('notify_url');
            //$notify_url ="http://property.wunengkeji.com/notify.php/syncnotify";
            if (!empty($notify_url)) {
                $s = $this->http_post($notify_url, $data);
            }
        }


    }

    private function http_post($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
            $is_curlFile = true;
        } else {
            $is_curlFile = false;
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        if (is_string($param)) {
            $strPOST = $param;
        } elseif ($post_file) {

            if ($is_curlFile) {
                foreach ($param as $key => $val) {

                    if (substr($val, 0, 1) == '@') {

                        $param[$key] = new \CURLFile(realpath(substr($val, 1)));

                    }
                }

            }
            $strPOST = $param;

        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);

        }

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type:application/json; charset=utf-8',

            'Content-Length: ' . strlen($param)));
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);

        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

}